home *** CD-ROM | disk | FTP | other *** search
/ Libris Britannia 4 / science library(b).zip / science library(b) / DJGPP / DJSRC111.ZIP / go32 / coff2exe.c < prev    next >
C/C++ Source or Header  |  1993-07-24  |  3KB  |  132 lines

  1. /* This is file AOUT2EXE.C */
  2. /*
  3. ** Copyright (C) 1993 DJ Delorie, 24 Kirsten Ave, Rochester NH 03867-2954
  4. **
  5. ** This file is distributed under the terms listed in the document
  6. ** "copying.dj", available from DJ Delorie at the address above.
  7. ** A copy of "copying.dj" should accompany this file; if not, a copy
  8. ** should be available from where this file was obtained.  This file
  9. ** may not be distributed without a verbatim copy of "copying.dj".
  10. **
  11. ** This file is distributed WITHOUT ANY WARRANTY; without even the implied
  12. ** warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  13. */
  14.  
  15. #include <stdio.h>
  16. #include <stdlib.h>
  17. #include <fcntl.h>
  18. #include <sys/stat.h>
  19. #include <alloc.h>
  20. #include <string.h>
  21. #include <io.h>
  22.  
  23. #include "stubbyte.h"
  24.  
  25. int stub_file = -1;
  26. long stub_length = 0;
  27.  
  28. unsigned long align_hsize(unsigned long s, unsigned char *h)
  29. {
  30.   long hsize = (s+511) & ~511;
  31.   h[2] = 0;
  32.   h[3] = 0;
  33.   h[4] = ( hsize / 512 ) & 0xff;
  34.   h[5] = ( hsize / 512 ) >> 8;
  35.   return hsize;
  36. }
  37.  
  38. void aout2exe(char *fname)
  39. {
  40.   int ifile;
  41.   int ofile;
  42.   char *ofname;
  43.   char buf[4096];
  44.   int rbytes;
  45.   long hsize;
  46.  
  47.   ifile = open(fname, O_RDONLY|O_BINARY);
  48.   if (ifile < 0)
  49.   {
  50.     perror(fname);
  51.     return;
  52.   }
  53.  
  54.   ofname = strrchr(fname, '/');
  55.   if (ofname == 0)
  56.     ofname = strrchr(fname, '\\');
  57.   if (ofname == 0)
  58.     ofname = fname;
  59.   ofname = strrchr(ofname, '.');
  60.   if (ofname) *ofname = 0;
  61.  
  62.   ofname = (char *)malloc(strlen(fname)+5);
  63.   sprintf(ofname, "%s.exe", fname);
  64.   ofile = open(ofname, O_WRONLY|O_CREAT|O_TRUNC|O_BINARY, 0666);
  65.   if (ofile < 0)
  66.   {
  67.     perror(ofname);
  68.     free(ofname);
  69.     return;
  70.   }
  71.  
  72.   if (stub_length)
  73.   {
  74.     long left = stub_length;
  75.     long wrote = 0;
  76.     lseek(stub_file, 0L, 0);
  77.     while (left)
  78.     {
  79.       char buf[512];
  80.       int r = _read(stub_file, buf, 512);
  81.       if (wrote == 0)
  82.         hsize = align_hsize(stub_length, (unsigned char *)buf);
  83.       _write(ofile, buf, r);
  84.       left -= r;
  85.       wrote += r;
  86.     }
  87.     _write(ofile, buf, 512-((int)wrote&511));
  88.   }
  89.   else
  90.   {
  91.     hsize = align_hsize(sizeof(stub_bytes), (unsigned char *)stub_bytes);
  92.     write(ofile, stub_bytes, (int)hsize);
  93.   }
  94.   
  95.   while ((rbytes=read(ifile, buf, 4096)) > 0)
  96.   {
  97.     int wb = write(ofile, buf, rbytes);
  98.     if (wb < 0)
  99.     {
  100.       perror(ofname);
  101.       break;
  102.     }
  103.     if (wb < rbytes)
  104.     {
  105.       fprintf(stderr, "%s: disk full\n", ofname);
  106.       exit(1);
  107.     }
  108.   }
  109.   close(ifile);
  110.   close(ofile);
  111.   free(ofname);
  112. /*  remove(fname); */
  113. }
  114.  
  115. int main(int argc, char **argv)
  116. {
  117.   int i;
  118.   if (argc > 2 && strcmp(argv[1], "-s") == 0)
  119.   {
  120.     struct stat s;
  121.     stub_file = _open(argv[2], O_RDONLY);
  122.     fstat(stub_file, &s);
  123.     stub_length = s.st_size;
  124.     argc -= 2;
  125.     argv += 2;
  126.   }
  127.   for (i=1; i<argc; i++)
  128.     aout2exe(argv[i]);
  129.   return 0;
  130. }
  131.  
  132.